package jdbc.three.controller;

import jdbc.day2.demo4_对象工厂.ObjectFactory;
import jdbc.three.beans.Account;
import jdbc.three.beans.Request;
import jdbc.three.service.AccountService;

import java.sql.SQLException;

public class AccountCtrl {
    AccountService service= (AccountService) ObjectFactory.getObject("accountServiceProxy");
    public String register(Request r){
        Account a=new Account();
        a.setId(Integer.parseInt(r.getParameter("id")));
        a.setMoney(Double.parseDouble(r.getParameter("money")));
        a.setName(r.getParameter("name"));
        boolean b=false;
        try{
            b=service.register(a);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return b?"注册成功":"注册失败";
    }

    public double queryMoney(Request r){
        double rs=0;
        try{
            rs=service.queryMoney(Integer.parseInt(r.getParameter("id")));
        }catch (Exception e){
            e.printStackTrace();
        }
        return rs;
    }

    public String transfer(Request r){
        int from= Integer.parseInt(r.getParameter("from"));
        int to= Integer.parseInt(r.getParameter("to"));
        double m=Double.parseDouble(r.getParameter("money"));
        boolean rs=false;
        try{
            rs=service.transferMoney(from,to,m);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  rs?"转帐成功":"转帐失败";
    }

}
